﻿package net.wangit.jdbc.executable;

import java.util.Iterator;
import java.util.List;

import net.wangit.jdbc.table.Column;


public class SQLUtil {

	private SQLUtil() {
	}

	/**
	 * 生成insert SQL语句
	 * 
	 * @param tableName表名
	 * @param columns列集合
	 * @return
	 */
	public static String toInsertStatement(String tableName,
			List<Column> columns) {
		StringBuffer head = new StringBuffer();
		StringBuffer tail = new StringBuffer();
		head.append("insert into ").append(tableName);
		head.append(" (");
		Iterator<Column> iter = columns.iterator();
		while (iter.hasNext()) {
			Column column = iter.next();
			head.append(column.getName());
			tail.append("?");
			if (iter.hasNext()) {
				head.append(", ");
				tail.append(", ");
			}
		}
		
		return head.append(") values (").append(tail).append(')').toString();
	}

	/**
	 * 生成insert SQL语句
	 * 
	 * @param tableName表名
	 * @param columns列名
	 * @return
	 */
	public static String toInsertStatement(String tableName, String[] columns) {
		StringBuffer head = new StringBuffer();
		StringBuffer tail = new StringBuffer();
		head.append("insert into ").append(tableName);
		head.append(" (");
		for (int i = 0; i < columns.length; i++) {
			head.append(columns[i]);
			tail.append("?");
			if (i < columns.length - 1) {
				head.append(", ");
				tail.append(", ");
			}
		}
		
		return head.append(") values (").append(tail).append(')').toString();
	}

	/**
	 * 生成update SQL语句
	 * 
	 * @param tableName表名
	 * @param columns列集合
	 * @param primaryKeyColumns主键集合（也可以是定位的多个列）
	 * @return
	 */
	public static String toUpdateStatement(String tableName,
			List<Column> columns, List<Column> primaryKeyColumns) {
		StringBuffer buf = new StringBuffer(columns.size() * 15
				+ tableName.length() + 10);
		// 更新字段
		buf.append("update ").append(tableName).append(" set ");
		Iterator<Column> iter = columns.iterator();
		while (iter.hasNext()) {
			Column column = iter.next();
			buf.append(column.getName()).append("=?");
			if (iter.hasNext()) {
				buf.append(", ");
			}
		}

		// 定位条件
		buf.append(" where ");
		iter = primaryKeyColumns.iterator();
		while (iter.hasNext()) {
			Column column = iter.next();
			buf.append(column.getName()).append("=?");
			if (iter.hasNext()) {
				buf.append(" and ");
			}
		}

		return buf.toString();
	}

	/**
	 * 生成delete Sql语句
	 * 
	 * @param tableName表名
	 * @param primaryKeyColumns主键集合（也可以是定位的多个列）
	 * @return
	 */
	public static String toDeleteStatement(String tableName,
			List<Column> primaryKeyColumns) {
		StringBuffer buf = new StringBuffer(tableName.length() + 10);
		buf.append("delete from ").append(tableName).append(" where ");
		Iterator<Column> iter = primaryKeyColumns.iterator();
		while (iter.hasNext()) {
			Column column = iter.next();
			buf.append(column.getName()).append("=?");
			if (iter.hasNext()) {
				buf.append(" and ");
			}
		}

		return buf.toString();
	}
}
